/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.model.uml.Kernel;
// imports:
import java.util.List;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.common.MetaTypedList;
import edu.pku.sei.modeler.model.core.CorePackage;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;
import edu.pku.sei.modeler.uml.util.MetaTypedSet;



/**
 * @generated
 * 
 * modified by zsj
 */
public class StructuralFeature extends Feature implements IStructuralFeature {
	// attributes
	
	protected boolean isReadOnly;
	
	
	
	protected boolean isOrdered;
	
	protected boolean isUnique;
	
	protected int upper;
	
	protected int lower;
	
	
	// references
	
	
	
	protected MetaTypedList upperValue;
	
	protected MetaTypedList lowerValue;
	
	//modified by zsj; change MetaTypedList to Classifier
	protected Classifier type;
	
	
	//added by zsj for type; it's not in the specification
	//FIXME
	private int typeId;
	//end of adding
	
	// getters & setters for attributes
	
	@MethodProperty(
			id = "STRUCTURALFEATURE_ISREADONLY",
			catalog = "General",
			name = ModelPackage.STRUCTURALFEATURE_ISREADONLY,
			setter = "setIsReadOnly"
			)
	
	public boolean getIsReadOnly() {
		return isReadOnly;
	}
	public void setIsReadOnly(boolean newIsReadOnly) {
		boolean oldisReadOnly = isReadOnly;
		isReadOnly = newIsReadOnly;
		firePropertyChange(ModelPackage.STRUCTURALFEATURE_ISREADONLY, oldisReadOnly, newIsReadOnly);
	}
	
	@MethodProperty(
			id = "STRUCTURALFEATURE_ISORDERED",
			catalog = "General",
			name = ModelPackage.MULTIPLICITYELEMENT_ISORDERED,
			setter = "setIsOrdered"
			)
	
	public boolean getIsOrdered() {
		return isOrdered;
	}
	public void setIsOrdered(boolean newIsOrdered) {
		boolean oldisOrdered = isOrdered;
		isOrdered = newIsOrdered;
		firePropertyChange(ModelPackage.MULTIPLICITYELEMENT_ISORDERED, oldisOrdered, newIsOrdered);
	}
	
	@MethodProperty(
			id = "STRUCTURALFEATURE_ISUNIQUE",
			catalog = "General",
			name = ModelPackage.MULTIPLICITYELEMENT_ISUNIQUE,
			setter = "setIsUnique"
			)
	
	public boolean getIsUnique() {
		return isUnique;
	}
	public void setIsUnique(boolean newIsUnique) {
		boolean oldisUnique = isUnique;
		isUnique = newIsUnique;
		firePropertyChange(ModelPackage.MULTIPLICITYELEMENT_ISUNIQUE, oldisUnique, newIsUnique);
	}
	
	@MethodProperty(
			id = "STRUCTURALFEATURE_UPPER",
			catalog = "General",
			name = ModelPackage.MULTIPLICITYELEMENT_UPPER,
			setter = "setUpper"
			)
	
	public int getUpper() {
		return upper;
	}
	public void setUpper(int newUpper) {
		int oldupper = upper;
		upper = newUpper;
		firePropertyChange(ModelPackage.MULTIPLICITYELEMENT_UPPER, oldupper, newUpper);
	}
	
	@MethodProperty(
			id = "STRUCTURALFEATURE_LOWER",
			catalog = "General",
			name = ModelPackage.MULTIPLICITYELEMENT_LOWER,
			setter = "setLower"
			)
	
	public int getLower() {
		return lower;
	}
	public void setLower(int newLower) {
		int oldlower = lower;
		lower = newLower;
		firePropertyChange(ModelPackage.MULTIPLICITYELEMENT_LOWER, oldlower, newLower);
	}
	
	// getters for references
	
	public MetaTypedList getUpperValue() {
		if(upperValue == null) {
			upperValue = new MetaTypedSet(ValueSpecification.class, this, ModelPackage.MULTIPLICITYELEMENT_UPPERVALUE);
		}
		return upperValue;
	}
	
	public MetaTypedList getLowerValue() {
		if(lowerValue == null) {
			lowerValue = new MetaTypedSet(ValueSpecification.class, this, ModelPackage.MULTIPLICITYELEMENT_LOWERVALUE);
		}
		return lowerValue;
	}
	@MethodProperty(id = "TYPEDELEMENT_TYPE", catalog = "General", name = ModelPackage.TYPEDELEMENT_TYPE, setter = "setType")
	@EnumerationProperty(function = "getTypes")
	public Classifier getTypeAsClassifier() {
		if(type == null) {
			type = (Classifier) XMLUtility.xmlInstance.getMetaElementByID(typeId);
		}
		return type;
	}
	
	
	public MetaTypedList getType() {
		return null;
	}

	
	//added by zsj
	public Object[] getTypes() {
		List types = model.getAllTypes();
		return types.toArray();
	}
	
	public void setType(Classifier newType) {
		Classifier oldType = this.type;
		this.type = newType;
		this.firePropertyChange(ModelPackage.TYPEDELEMENT_TYPE, oldType, newType);

	}
	//end 
	
	// save & load
	public void serialize(Element root) {
		super.serialize(root);
		// attributes
		
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.STRUCTURALFEATURE_ISREADONLY, isReadOnly);
		
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.MULTIPLICITYELEMENT_ISORDERED, isOrdered);
		
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.MULTIPLICITYELEMENT_ISUNIQUE, isUnique);
		
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.MULTIPLICITYELEMENT_UPPER, upper);
		
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.MULTIPLICITYELEMENT_LOWER, lower);
		
		// references
		
		XMLUtility.xmlInstance.serializeNodes(root, ModelPackage.MULTIPLICITYELEMENT_UPPERVALUE, upperValue);
		
		XMLUtility.xmlInstance.serializeNodes(root, ModelPackage.MULTIPLICITYELEMENT_LOWERVALUE, lowerValue);
		
		//why? --fengchao
		
		//modified by zsj to fix the recursive bug
		//XMLUtility.xmlInstance.serializeNodesNotContain(root, ModelPackage.TYPEDELEMENT_TYPE, type);	
//		if (this.getType() != null) {
//			XMLUtility.xmlInstance.serializeAndCreateNode(root, ModelPackage.TYPEDELEMENT_TYPE, new String[] { CorePackage.ELEMENT_ELEMENTID },
//					new String[] { String.valueOf(this.getTypeAsClassifier().getElementID()) });
//		}

		//end 
		XMLUtility.xmlInstance.serializeNodeNotContain(root, ModelPackage.TYPEDELEMENT_TYPE, this.getTypeAsClassifier());
		// fengchao modify end
	}
	
	public void deSerialize(Element root) {
		super.deSerialize(root);
		// attributes
		
		isReadOnly = XMLUtility.xmlInstance.deSerializeBoolean(root, ModelPackage.STRUCTURALFEATURE_ISREADONLY);
		
		isOrdered = XMLUtility.xmlInstance.deSerializeBoolean(root, ModelPackage.MULTIPLICITYELEMENT_ISORDERED);
		
		isUnique = XMLUtility.xmlInstance.deSerializeBoolean(root, ModelPackage.MULTIPLICITYELEMENT_ISUNIQUE);
		
		upper = XMLUtility.xmlInstance.deSerializeInt(root, ModelPackage.MULTIPLICITYELEMENT_UPPER);
		
		lower = XMLUtility.xmlInstance.deSerializeInt(root, ModelPackage.MULTIPLICITYELEMENT_LOWER);
		
		// references
		
		this.getUpperValue().addAll(XMLUtility.xmlInstance.deSerializeNodes(root, ModelPackage.MULTIPLICITYELEMENT_UPPERVALUE));
		
		this.getLowerValue().addAll(XMLUtility.xmlInstance.deSerializeNodes(root, ModelPackage.MULTIPLICITYELEMENT_LOWERVALUE));
		
		//this.getType().addAll(XMLUtility.xmlInstance.deSerializeNodesNotContain(root, ModelPackage.TYPEDELEMENT_TYPE, this));
		this.typeId = XMLUtility.xmlInstance.getIDAttribute(root, ModelPackage.TYPEDELEMENT_TYPE);
		//fengchao modify
		this.setType((Classifier) XMLUtility.xmlInstance.deSerializeNodeNotContain(root, ModelPackage.TYPEDELEMENT_TYPE, this));
	}
	
	public void dynamicSet(String featureID, Object value) {
		if(featureID == null || "".equals(featureID)) {
			return;
		}
		// attributes
		
		// references
		
		else if(ModelPackage.TYPEDELEMENT_TYPE.equals(featureID)) {
			type = (Classifier) value;
		}
		
		else
			super.dynamicSet(featureID, value);
	}
}
 
 
 
 
 